cmake_minimum_required(VERSION 3.16)

project(CMake_music VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (MSVC)
    add_compile_options(/Zc:__cplusplus)
endif ()

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets multimedia sql)  # 添加 Multimedia
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets multimedia sql)

# 设置头文件和源文件目录
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

set(PROJECT_SOURCES
        src/main.cpp
        src/music.cpp
        include/music.h
        src/music.ui
)

if (${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(CMake_music
            MANUAL_FINALIZATION
            ${PROJECT_SOURCES}
            resource.qrc
            include/btform.h src/btform.cpp src/btform.ui
            include/recbox.h src/recbox.cpp src/recbox.ui
            include/recboxitem.h src/recboxitem.cpp src/recboxitem.ui
            include/commonpage.h src/commonpage.cpp src/commonpage.ui
            include/listitembox.h src/listitembox.cpp src/listitembox.ui
            include/musicslider.h src/musicslider.cpp src/musicslider.ui
            include/volumetool.h src/volumetool.cpp src/volumetool.ui
            include/musiclist.h src/musiclist.cpp
            include/musicfile.h src/musicfile.cpp
            include/qmediaplaylist.h src/qmediaplaylist.cpp
            include/lyricspage.h src/lyricspage.cpp src/lyricspage.ui
            include/lyricspage.h
    )
    # Define target properties for Android with Qt 6 as:
    #    set_property(TARGET CMake_music APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
    #                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
    # For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else ()
    if (ANDROID)
        add_library(CMake_music SHARED
                ${PROJECT_SOURCES}
        )
        # Define properties for Android with Qt 5 after find_package() calls as:
        #    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else ()
        add_executable(CMake_music
                ${PROJECT_SOURCES}
        )
    endif ()
endif ()

#
target_include_directories(CMake_music PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_link_libraries(CMake_music PRIVATE
        Qt${QT_VERSION_MAJOR}::Widgets
        Qt6::Multimedia  # 必须添加！
        Qt${QT_VERSION_MAJOR}::Sql
)
# 添加绝对路径到头文件搜索路径
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if (${QT_VERSION} VERSION_LESS 6.1.0)
    set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.CMake_music)
endif ()
set_target_properties(CMake_music PROPERTIES
        ${BUNDLE_ID_OPTION}
        MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
        MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
        MACOSX_BUNDLE TRUE
        WIN32_EXECUTABLE FALSE
)

include(GNUInstallDirs)
install(TARGETS CMake_music
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if (QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(CMake_music)
endif ()
